<template>
	<view>
		<image src="../../static/image/logo_hmj.png" mode="widthFix" class="w280 auto mt100"></image>
		<view class="tf22 tf_333 mt48 mb100 tac">红马甲 服务到家</view>
		<button type='primary' open-type="getUserInfo" @click="getUserInfo" class="fz30 radius8 tac w690 lh88 auto" style="background-color: #f14537;">微信授权一键登录</button>
	</view>
</template>

<script>
	import { getCode,UserInfo } from '@/config/api.js';
	export default {
		data() {
			return {
				openid:'',
				sessionKey:'',
			}
		},
		onLoad() {
			this.get_code()
		},
		methods: {
			getUserInfo(){
				uni.getUserProfile({
					desc: '用于完善会员资料',
					success: infoRes => {
						UserInfo({   //获取用户基本信息
							iv: infoRes.iv,
							openid: this.openid,
							sessionKey: this.sessionKey,
							encryptedData: infoRes.encryptedData
						}).then((res) => {
							console.log(res)
							uni.setStorageSync('user_info', res);
							uni.setStorageSync('token', res.token);
							uni.navigateBack({
								delta: 1
							})
						})
					}
				});
			},
			// 登录
			get_code() {
				uni.login({
					provider: 'weixin',
					success: loginRes => {
						console.log(loginRes.code)
						getCode({ code: loginRes.code }).then((res) => {  //用code换openid和sessionKey
						console.log(res)
						this.openid = res.openid
						this.sessionKey = res.session_key
						})
					}
				});
			}
		}
	}
</script>

<style>

</style>

